﻿using TopHero;
using UnityEngine;
using Vector2 = TopHero.Vector2;

namespace View
{
    public class PrefabUnityObject : UnityObject
    {
        public int prefabId;
        
        private GameObject _Obj;

        public override void Link(BaseEntity gameEntity)
        {
            base.Link(gameEntity);
            Vector2 pos = gameEntity.GetPosition();
            transform.position = new Vector3(pos.x, pos.y, 0);

            if (prefabId == 2)
            {
                GameObject prefab = Resources.Load<GameObject>("Monster/monster");
                _Obj = Instantiate(prefab);
                _Obj.transform.parent = transform;
                _Obj.transform.localPosition = Vector3.zero;
                _Obj.transform.localScale = Vector3.one * 0.4f;
            }
        }

        private void OnDrawGizmos()
        {
            if (prefabId == 1)
            {
                // 火把
                Gizmos.color = Color.white;
                Vector3 pos = new Vector3(transform.position.x, transform.position.y, 1.0f);
                UnityUtility.DrawRectangle(pos, new Vector3(0.95f, 0.95f, 0));
                UnityUtility.DrawCircle(pos, 0.45f);

                if (_linkedEntity != null)
                {
                    Vector2 position = _linkedEntity.GetPosition();
                    int index = GameController.Instance.game.world.PositionToIndex(position);
                    GUIStyle style = new GUIStyle();
                    style.normal.textColor = Color.magenta;
                    style.fontSize = 10;
                    UnityEditor.Handles.Label(new Vector3(position.x, position.y, 1), new GUIContent(index.ToString()),
                        style);
                }
            }
        }
    }
}